import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxConfig;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import com.yoke.util.StringUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
public class Nfinx {
    public static void main(String[] u) throws IOException {
//        NgxConfig conf = NgxConfig.read("D:\\etc\\nginx\\conf.d\\ad\\lk\\0109.conf");
//        List<NgxEntry> locations = conf.findAll(NgxConfig.BLOCK, "server", "location");
//        if (locations != null)
//            for (NgxEntry ngxEntry : locations) {
//                if (ngxEntry instanceof NgxBlock) {
//                    NgxBlock block = (NgxBlock) ngxEntry;
//                    String loc_url = block.getValue();
//                    if ("/".equals(loc_url)) {
//                        NgxParam ngxParam = block.findParam("root");
//                        System.out.println(ngxParam.getValue());
//                    }
//                    System.out.println();
//                }
//            }
//        //System.out.println(locations);
        loc();
        //delRewrite("/js_aliyun");
        // createRewrite("/dsfsdf/", "sdfsdf");
    }

    private static void loc() throws IOException {
        NgxConfig conf = NgxConfig.read("D:\\etc\\nginx\\conf.d\\ad\\lk\\dswys_0109.conf");
        System.out.println(conf.findBlock("server"));
        List<NgxEntry> locations = conf.findAll(NgxConfig.BLOCK, "server", "location");
        if (locations != null)
            for (NgxEntry ngxEntry : locations) {
                if (ngxEntry instanceof NgxBlock) {
                    NgxBlock block = (NgxBlock) ngxEntry;
                    NgxParam ngxParam = block.findParam("rewrite");
                    if (ngxParam != null) {
                        System.out.println(ngxParam.getValues().get(1));

                    }
                }
            }
        //System.out.println(locations);
    }

    private static void createRewrite(String loc, String toUrl) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\tlocation ").append(loc).append(" ").append("{").append("\t\n");
        stringBuffer.append("\t\t").append("rewrite \"^/(.*)$\" ").append(toUrl).append("$2").append(" ").append("break;").append("\t\n");
        stringBuffer.append("\t}").append("\t\n");
        System.out.println(stringBuffer);
        appendRewrite("D:\\etc\\nginx\\conf.d\\ad\\lk\\0109.conf", stringBuffer);
    }

    private static void appendRewrite(String file, CharSequence info) {
        StringBuffer stringBuffer = getBuffer(file);
        int end = stringBuffer.lastIndexOf("}");
        stringBuffer.insert(end - 1, info);
        System.out.println(stringBuffer);
    }

    private static StringBuffer getBuffer(String file) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(new File(file)));
            String line;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line).append(System.lineSeparator());
            }
            return stringBuffer;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static void delRewrite(String loc) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(new File("D:\\etc\\nginx\\conf.d\\ad\\lk\\0109.conf")));
            String line;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(loc)) {
                    if (!line.endsWith("}"))
                        while ((line = bufferedReader.readLine()) != null) {
                            if (line.endsWith("}"))
                                break;
                        }
                } else {
                    if (StringUtil.IsEmpty(line))
                        continue;
                    stringBuffer.append(line).append(System.lineSeparator());
                }
            }
            System.out.println(stringBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
